<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- <el-date-picker v-model="listQuery.record_date_start" style="width:140px;" format="yyyy-MM-dd" type="datetime" placeholder="跟进日期" /> -->

      <el-input
        v-model="listQuery.real_name"
        placeholder="客户姓名"
        style="width: 110px;;margin-top:8px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />

      <company ref="companys"></company>
      <el-select v-model="listQuery.shareholder" placeholder="股东" class="filter-item" style="width:130px;margin-top:8px">
        <el-option label="股东" value="" />
        <el-option
          v-for="item in shareholderList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>

      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" style="margin-top:8px;" @click="handleFilter">
        查询
      </el-button>
      <excelout />
      <!-- <el-badge :value="notice" class="item" type="warning" style="margin-top:-3px;margin-left:10px" v-if="notice!=0" >
        <el-button @click="getNotice()">系统提醒</el-button>
      </el-badge> -->

    </div>

    <el-table :key="tableKey" v-loading="listLoading" :element-loading-text="loadingtext" :data="list" :row-class-name="tableRowClassName1" size="mini" border fit highlight-current-row style="width: 100%;">
      <!-- <el-table-column type="selection" width="45" align="center" ></el-table-column> -->
      <el-table-column label="操作" align="center" width="280" class-name="small-padding fixed-width">
        <template v-if="row.is_paid!=1" slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="record(row.order_id)">
            跟进记录
          </el-button>
          <el-button type="success" size="mini" @click="plan(row.order_id)">
            租金计划
          </el-button>
          <el-button type="info" size="mini" @click="detail(row.order_id)">
            详情
          </el-button>
        </template>
      </el-table-column>

      <el-table-column label="租车日期" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.create_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="门店" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.c_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="客户姓名" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.real_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号码" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.mobile }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="签单人" class-name="status-col" width="100">
        <template slot-scope="{row}">
          {{ row.sale_name }}
        </template>
      </el-table-column> -->
      <el-table-column label="融资金额" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.total_price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="期数" align="center" width="60">
        <template slot-scope="{row}">
          <span>{{ row.total_period }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已付期数" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.paid_number }}</span>
        </template>
      </el-table-column>
      <el-table-column label="已付金额" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.paid_money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="剩余金额" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.no_paid_money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="剩余期数" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.no_paid_number }}</span>
        </template>
      </el-table-column>

      
      
      <el-table-column label="累计逾期天数" class-name="status-col" width="100">
        <template slot-scope="{row}">
          {{ row.all_overdue_days }}
        </template>
      </el-table-column>

    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList(listQuery)" />
    <el-dialog title="租金计划" :visible.sync="dialogPlanAble" style="width:1500px;margin:auto">
      <table class="flowtable">
        <tr>
          <th>期数</th>
          <th>应付日期</th>
          <th>应付金额</th>
          <th>实付金额</th>
          <th>实付日期</th>
          <th>逾期天数</th>
          <th>付租状态</th>
        </tr>
        <tr v-for="(item,index) in plan_list" :key="index">
          <td>{{ item.num }}</td>
          <td>{{ item.time_date }}</td>
          <td>{{ item.total_money }}</td>
          <td>{{ item.paid_money }}</td>
          <td>{{ item.sh_time }}</td>
          <td>{{ item.late_num }}</td>
          <td>{{ item.status_name }}</td>
        </tr>
      </table>
      <pagination v-show="plan_total>0" :total="plan_total" :page.sync="planQuery.page" :limit.sync="planQuery.limit" @pagination="planList" />
    </el-dialog>
    <el-dialog :visible.sync="dialogRecordAble" class="abow_dialog" title="跟进记录" width="750px">
      <collectForm v-if="addStatus" :order_id="form.order_id" @cancel="addStatus=false;" />
      <div class="radio">
        跟进记录：
        <el-button v-if="addStatus==false" type="primary" round size="mini" @click="addStatus=true">新增记录</el-button>
      </div>
      <collectRecord v-if="dialogRecordAble&&addStatus==false" :order_id="form.order_id" />
    </el-dialog>

  </div>
</template>

<script>
import {
  collectRecordAdd,
  getOrderHistoryPaymentOverList,
  sendTipsByAdmin,
  getCollectWay,
  overdueExcelout,
  tagProsecution
} from '@/api/collection'
import excelout from '@/components/excelout/index.vue'
import {
  paymentList, getRegion, backendgetmerchantregion,getShareHolder
} from '@/api/finance'

import { merchantListOnlyMerchant } from '@/api/merchant'
import collectRecord from '@/components/collect/index.vue'
import collectForm from '@/components/collect/form.vue'
import { fetchList } from '@/api/sale'
import company from '@/components/company/company.vue' 
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'NormalTable',
  components: {
    Pagination,
    collectRecord,
    collectForm,
    excelout,company
  },
  directives: {
    waves
  },
  data() {
    return {
      loadingtext: '数据加载中...',
      action: process.env.VUE_APP_BASE_API + 'upload/file',
      myHeaders: {
        token: this.$store.getters.token
      },
      shareQuery: {
        limit: 100
      },
      company_list: [],
      tableKey: 0,
      addStatus: false,
      tableRecord: 1,
      list: null,
      plan_list: [],
      orderinfo: [],
      way_list: [],
      status_filters: [{ text: '已还', value: '1' }, { text: '未还', value: '1,2' }],
      order_ids: [],
      listRecordLoading: true,
      recordTotal: 0,
      total: 0,
      plan_total: 0,
      dialogPlanAble: false,
      dialogRecordAble: false,
      listLoading: true,
      planQuery: {
        page: 1,
        limit: 10
      },
      listQuery: {
        page: 1,
        limit: 10,
        real_name: '',
        c_id: '',
        shareholder: ''
      },
      notice: 0,
      system_admin_list: [],
      order_id: 0,
      dialogFormVisible: false,
      dialogRecordVisible: false,
      dialogStatus: '',
      downloadLoading: false,
      reverse: false,
      form: {
        way: [],
        result: '',
        desc: '',
        order_id: ''
      },
      sale_list: [],
      shareholderList: []
    }
  },
  created() {
    //this.cacheQueryFormat()
    this.getList(this.listQuery)
    // this.getWay();
    // this.getSaleList();
    this.getShareHolder()
    this.getCompanyList()
    // 添加钩子，页面刷新
    //window.addEventListener('beforeunload', e => this.beforeunloadFn(e))
  },
  destroyed() {
    // 添加钩子，页面刷新
    window.removeEventListener('beforeunload', e => this.beforeunloadFn(e))
  },
  methods: {
    getWay() {
      getCollectWay().then(response => {
        this.way_list = response.data
      })
    },
    getShareHolder(order_id) {
      getShareHolder(this.shareQuery).then(response => {
        this.shareholderList = response.data.data
      })
      this.getThisOrderShareHolder(order_id)
    },

    tableRowClassName1({ row, rowIndex }) {
      if (row.color) {
        return 'warning-row'
      } else {
        // return 'default-row';
      }
    },
    prosecution(order_id, lab) {
      const pam = { order_id: order_id, lab: lab }
      this.$alert('确定操作，不可撤回', '提示', {
        confirmButtonText: '确定',
        callback: action => {
          tagProsecution(pam).then(response => {
            if (response.code == 200) {
              this.$notify({
                title: '提示',
                message: '操作成功',
                type: 'success',
                duration: 1000,
                onClose: this.getList(this.listQuery)
              })
            }
          })
        }
      })
    },

    beforeunloadFn(e) {
      // 刷新页面事件
      sessionStorage.removeItem('overdue')
    },
    getList(query) {
      //sessionStorage.setItem('overdue', JSON.stringify(this.listQuery))
      this.listLoading = true
      getOrderHistoryPaymentOverList(query).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.notice = response.data.notice
        // this.sale_list = response.data.sale
        setTimeout(() => {
          this.listLoading = false
        }, 1.0 * 200)
      })
    },
    detail(order_id) {
      this.$router.push('/order/detail/' + order_id)
    },
    cacheQueryFormat() {
      const cacheQuery = sessionStorage.getItem('overdue')
      if (cacheQuery) {
        this.listQuery = JSON.parse(cacheQuery)
      }
    },
    excelout() {
      this.loadingtext = '正在生成表格，请稍等...'
      this.listLoading = true
      this.listQuery.limit = this.total
      overdueExcelout(this.listQuery).then(response => {
        this.listLoading = false
        const filepath = response.data.path
        window.location.href = filepath
      })
    },
    getSaleList() {
      fetchList().then(response => {
        this.sale_list = response.data.data
      })
    },

    tableRowClassName({ row, rowIndex }) {
      if (row.s == 1) {
        return 'success-row'
      } else if (row.s == 2) {
        // return 'warning-row';
      }
      return ''
    },
    getCompanyList() {
      merchantListOnlyMerchant(this.listMerchantQuery).then(response => {
        this.company_list = response.data.company
        this.sale_list = response.data.sale.data
      })
    },

    distribution() {
      this.dialogFormVisible = true
    },
    handleFilter() {
      let c_id = this.$refs.companys.$data.listQuery.c_id;
      this.listQuery.c_id = c_id
      this.listQuery.page = 1
      this.getList(this.listQuery)
    },
    plan(order_id) {
      this.dialogPlanAble = true
      this.planQuery.order_id = order_id
      this.planList()
    },
    planList() {
      paymentList(this.planQuery).then(response => {
        this.plan_list = response.data.list.data
        this.plan_total = response.data.list.total
        this.listLoading = false
      })
    },
    detail(order_id) {
      this.$router.push('/order/detail/' + order_id)
    },
    record(order_id) {
      this.dialogRecordAble = true
      this.form.order_id = order_id
    }
  }
}
</script>

<style>
.collectcss{margin-top:7px;color:#999;font-size:14px}
.el-table .warning-row {
      background: orangered;
    }

  .el-table .success-row {
    background: #98dd72;
  }
  .item {
  margin-top: 10px;
}
.flowtable{
    width: 80%;margin-left: 10%;
    border: 1px solid #eee;margin-top: 20px;  border-collapse:collapse;
  }
  .flowtable tr {height: 40px;}
  .flowtable td,.flowtable th{
    text-align: center;
    border-collapse:collapse;border: 1px solid #eee;
  }
</style>
